// 以下JS函数用于获取url参数
// @param variable 需获取的参数key
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return (false);
}

var id = getQueryVariable("id");
var title = document.getElementById("add_title");
var category = document.getElementById("add_category");
var content = document.getElementById("add_content");


$.ajax({
    url: 'control/article.php',
    type: 'post',
    data: {
        "id": id
    },
    dataType: 'json',
    success: function (result) {
        jsonData = eval(result);
        title.value = jsonData.title;
        category.value = jsonData.category;
        content.innerText = jsonData.content;
    }
});

function submitArticle() {
    var edioTitle = document.getElementById("add_title").value;
    var edioCategory = document.getElementById("add_category").value;
    var edioContent = document.getElementById("add_content").innerText;
    var status = confirm("确认提交？");
    if (status) {
        $.ajax({
            url: 'http://localhost/Myblog/admin/control/edioArticle.php',
            type: 'post',
            data: {
                "id": id,
                "title": edioTitle,
                "category": edioCategory,
                "content": edioContent,
            },
            dataType: 'json',
            success: function (result) {
                if (result) {
                    alert("更新成功");
                    window.location.href = "index.html";
                }
            }
        })
    }
}